package com.framework.loippi.controller.api;

import com.framework.loippi.api.utils.ApiUtils;
import com.framework.loippi.entity.Category;
import com.framework.loippi.service.CategoryService;
import com.framework.loippi.utils.tool.CategoryUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName CategoryController
 * @Description TODO
 * @Author fsociety
 * @Date 12/12/2019 下午5:04
 * @Version 1.0
 */
@Controller("apiCategoryController")
@RequestMapping("/api/category")
@Api(tags = {"药品分类信息"})
public class CategoryController extends ApiBaseController {

    @Resource
    CategoryService categoryService;

    @RequestMapping(value = "/findAll.json", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    @ApiOperation(value = "查询所有药品分类", notes = "{}")
    public @ResponseBody String findAll() {
       List<Category> list = categoryService.findAll();
       List<Category> rootList = new ArrayList();

       for (Category o : list) {
           if  (o.getParentId() == 0) {
              rootList.add(o);
           }
       }

       for (Category p : rootList) {
           List<Category> subList = CategoryUtils.getSub(p.getId(), list);
           p.setSubList(subList);
       }
       return ApiUtils.success(rootList);
    }
}
